using System.Security.Cryptography;
using System.Text;
using System;
using System.IO;

namespace BToolkit
{
    public class AESUtils
    {
        private static string Key
        {
            get { return @"fANqOtuGLXmeKRz0dVctf25fI7wo4o40"; } //32位
        }

        private static string IV
        {
            get { return @"7MAIlToRV5uGDA40"; } //16位
        }

        /// <summary>
        /// AES加密
        /// </summary>
        public static string EncryptString(string inputStr, string key = null, string iv = null)
        {
            if (inputStr == null) return null;
            if (key == null) key = Key;
            if (iv == null) iv = IV;
            using (RijndaelManaged aesAlg = new RijndaelManaged())
            {
                aesAlg.KeySize = 256;
                aesAlg.BlockSize = 128;
                aesAlg.Padding = PaddingMode.PKCS7;
                aesAlg.Key = Encoding.UTF8.GetBytes(key);
                aesAlg.IV = Encoding.UTF8.GetBytes(iv);
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(inputStr);
                        }
                        return Convert.ToBase64String(msEncrypt.ToArray());
                    }
                }
            }
        }

        /// <summary>
        /// AES解密
        /// </summary>
        public static string DecryptString(string inputStr, string key = null, string iv = null)
        {
            if (inputStr == null) return null;
            if (key == null) key = Key;
            if (iv == null) iv = IV;
            using (RijndaelManaged aesAlg = new RijndaelManaged())
            {
                aesAlg.KeySize = 256;
                aesAlg.BlockSize = 128;
                aesAlg.Padding = PaddingMode.PKCS7;
                aesAlg.Key = Encoding.UTF8.GetBytes(key);
                aesAlg.IV = Encoding.UTF8.GetBytes(iv);
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(inputStr)))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            return srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
        }

        /// <summary>
        /// AES加密（支持大文件）
        /// </summary>
        public static byte[] EncryptBytes(byte[] inputData, string key = null, string iv = null)
        {
            if (inputData == null) return null;
            if (key == null) key = Key;
            if (iv == null) iv = IV;
            using (RijndaelManaged aesAlg = new RijndaelManaged())
            {
                aesAlg.KeySize = 256;
                aesAlg.BlockSize = 128;
                aesAlg.Padding = PaddingMode.PKCS7;
                aesAlg.Key = Encoding.UTF8.GetBytes(key);
                aesAlg.IV = Encoding.UTF8.GetBytes(iv);
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        csEncrypt.Write(inputData, 0, inputData.Length);
                        csEncrypt.FlushFinalBlock();
                        return msEncrypt.ToArray();
                    }
                }
            }
        }

        /// <summary>
        /// AES解密（支持大文件）
        /// </summary>
        public static byte[] DecryptBytes(byte[] inputData, string key = null, string iv = null)
        {
            if (inputData == null) return null;
            if (key == null) key = Key;
            if (iv == null) iv = IV;
            using (RijndaelManaged aesAlg = new RijndaelManaged())
            {
                aesAlg.KeySize = 256;
                aesAlg.BlockSize = 128;
                aesAlg.Padding = PaddingMode.PKCS7;
                aesAlg.Key = Encoding.UTF8.GetBytes(key);
                aesAlg.IV = Encoding.UTF8.GetBytes(iv);
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msDecrypt = new MemoryStream(inputData))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (MemoryStream msOutput = new MemoryStream())
                        {
                            csDecrypt.CopyTo(msOutput);
                            return msOutput.ToArray();
                        }
                    }
                }
            }
        }
    }
}